package com.rx.mgt.service;

import com.rx.mgt.dao.GroupDao;
import com.rx.mgt.dao.GroupMangerDao;
import com.rx.mgt.dao.UserInfoExtDao;
import com.rx.mgt.domain.Group;
import com.rx.mgt.domain.GroupManager;
import com.rx.mgt.domain.UserInfoExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by zxj on 2017/4/26.
 */
@Service
public class GroupService {

    @Autowired
    GroupDao groupDao;
    @Autowired
    GroupMangerDao groupMangerDao;
    @Autowired
    UserInfoExtDao userInfoExtDao;

    @Transactional
    public List<Group> findGroupByDomain(String domain) {
        return groupDao.findGroupByDomain(domain);
    }

    @Transactional
    public void saveGroup(Group group) {
        groupDao.save(group);
    }

    @Transactional
    public void updateGroup(Group group) {
        Group group1 = groupDao.get(group.getId());
        group1.setName(group.getName());
        groupDao.update(group1);
    }

    @Transactional
    public void delete(Long aLong) {
        Group group = groupDao.get(aLong);
        if(group.getIs_root()==1){
            return;
        }
        groupDao.delete(group);
    }

    @Transactional
    public List<UserInfoExt> queryGroupMembersByGroupId(String groupId) {
        Group group = groupDao.get(Long.valueOf(groupId));
        Long pid = group.getpId();
        if(0==pid.intValue()){
            return userInfoExtDao.getUserInfoByDomain(group.getDomain());
        }
        return userInfoExtDao.queryGroupMembersByGroupId(groupId);
    }
    @Transactional
    public List<GroupManager> queryGroupMangerList(String domain) {
        return groupMangerDao.queryGroupMangerList(domain);
    }

    @Transactional
    public void addGroupManger(GroupManager manager) {
         groupMangerDao.saveOrUpdate(manager);
    }

    @Transactional
    public GroupManager findGroupManagerById(Long aLong) {
        return groupMangerDao.get(aLong);
    }

    @Transactional
    public void deleteGroupManager(String id) {
        groupMangerDao.delete(groupMangerDao.get(Long.valueOf(id)));
    }
}
